fetcher: Move high level functions into "fetcher-util"
authorColin Walters <walters@verbum.org>
Fri, 23 Dec 2016 01:34:07 +0000 (20:34 -0500)
committerAtomic Bot <atomic-devel@projectatomic.io>
Wed, 4 Jan 2017 16:32:11 +0000 (16:32 +0000)
Conceptually these now lay on top of the core API, and don't reference libsoup.
This is preparation for libcurl porting, but it's also just generally better.

Closes: #636
Approved by: jlebon

Makefile-libostree.am
src/libostree/ostree-fetcher-util.c [new file with mode: 0644]
src/libostree/ostree-fetcher-util.h [new file with mode: 0644]
src/libostree/ostree-fetcher.c
src/libostree/ostree-fetcher.h
src/libostree/ostree-metalink.c
src/libostree/ostree-repo-pull.c

index a7d7b11b1e4fb95f2942410a19020d6d7ea76830..88210c75ce81af315a9410d414099b8c7c3d81b8 100644 (file)
@@ -161,6 +161,8 @@ if USE_LIBSOUP
 libostree_1_la_SOURCES += \
        src/libostree/ostree-fetcher.h \
        src/libostree/ostree-fetcher.c \
+       src/libostree/ostree-fetcher-util.h \
+       src/libostree/ostree-fetcher-util.c \
        src/libostree/ostree-metalink.h \
        src/libostree/ostree-metalink.c \
        $(NULL)
diff --git a/src/libostree/ostree-fetcher-util.c b/src/libostree/ostree-fetcher-util.c
new file mode 100644 (file)
index 0000000..bc97674
--- /dev/null
@@ -0,0 +1,140 @@
+/* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*-
+ *
+ * Copyright (C) 2017 Colin Walters <walters@verbum.org>
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, write to the
+ * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
+ * Boston, MA 02111-1307, USA.
+ */
+
+#include "config.h"
+
+#include <gio/gfiledescriptorbased.h>
+#include <gio/gunixoutputstream.h>
+
+#include "ostree-fetcher-util.h"
+#include "otutil.h"
+
+typedef struct
+{
+  GInputStream   *result_stream;
+  gboolean         done;
+  GError         **error;
+}
+  FetchUriSyncData;
+
+static void
+fetch_uri_sync_on_complete (GObject        *object,
+                            GAsyncResult   *result,
+                            gpointer        user_data)
+{
+  FetchUriSyncData *data = user_data;
+
+  (void)_ostree_fetcher_request_finish ((OstreeFetcher*)object,
+                                        result, NULL, &data->result_stream,
+                                        data->error);
+  data->done = TRUE;
+}
+
+gboolean
+_ostree_fetcher_mirrored_request_to_membuf (OstreeFetcher  *fetcher,
+                                            GPtrArray     *mirrorlist,
+                                            const char     *filename,
+                                            gboolean        add_nul,
+                                            gboolean        allow_noent,
+                                            GBytes         **out_contents,
+                                            guint64        max_size,
+                                            GCancellable   *cancellable,
+                                            GError         **error)
+{
+  gboolean ret = FALSE;
+  const guint8 nulchar = 0;
+  g_autoptr(GMemoryOutputStream) buf = NULL;
+  g_autoptr(GMainContext) mainctx = NULL;
+  FetchUriSyncData data;
+  g_assert (error != NULL);
+
+  data.result_stream = NULL;
+
+  if (g_cancellable_set_error_if_cancelled (cancellable, error))
+    return FALSE;
+
+  mainctx = g_main_context_new ();
+  g_main_context_push_thread_default (mainctx);
+
+  data.done = FALSE;
+  data.error = error;
+
+  _ostree_fetcher_request_async (fetcher, mirrorlist, filename, 0, max_size,
+                                 OSTREE_FETCHER_DEFAULT_PRIORITY, cancellable,
+                                 fetch_uri_sync_on_complete, &data);
+  while (!data.done)
+    g_main_context_iteration (mainctx, TRUE);
+
+  if (!data.result_stream)
+    {
+      if (allow_noent)
+        {
+          if (g_error_matches (*error, G_IO_ERROR, G_IO_ERROR_NOT_FOUND))
+            {
+              g_clear_error (error);
+              ret = TRUE;
+              *out_contents = NULL;
+            }
+        }
+      goto out;
+    }
+
+  buf = (GMemoryOutputStream*)g_memory_output_stream_new (NULL, 0, g_realloc, g_free);
+  if (g_output_stream_splice ((GOutputStream*)buf, data.result_stream,
+                              G_OUTPUT_STREAM_SPLICE_CLOSE_SOURCE,
+                              cancellable, error) < 0)
+    goto out;
+
+  if (add_nul)
+    {
+      if (!g_output_stream_write ((GOutputStream*)buf, &nulchar, 1, cancellable, error))
+        goto out;
+    }
+
+  if (!g_output_stream_close ((GOutputStream*)buf, cancellable, error))
+    goto out;
+
+  ret = TRUE;
+  *out_contents = g_memory_output_stream_steal_as_bytes (buf);
+ out:
+  if (mainctx)
+    g_main_context_pop_thread_default (mainctx);
+  g_clear_object (&(data.result_stream));
+  return ret;
+}
+
+/* Helper for callers who just want to fetch single one-off URIs */
+gboolean
+_ostree_fetcher_request_uri_to_membuf (OstreeFetcher  *fetcher,
+                                       OstreeFetcherURI *uri,
+                                       gboolean        add_nul,
+                                       gboolean        allow_noent,
+                                       GBytes         **out_contents,
+                                       guint64        max_size,
+                                       GCancellable   *cancellable,
+                                       GError         **error)
+{
+  g_autoptr(GPtrArray) mirrorlist = g_ptr_array_new ();
+  g_ptr_array_add (mirrorlist, uri); /* no transfer */
+  return _ostree_fetcher_mirrored_request_to_membuf (fetcher, mirrorlist, NULL,
+                                                     add_nul, allow_noent,
+                                                     out_contents, max_size,
+                                                     cancellable, error);
+}
diff --git a/src/libostree/ostree-fetcher-util.h b/src/libostree/ostree-fetcher-util.h
new file mode 100644 (file)
index 0000000..0f25dc3
--- /dev/null
@@ -0,0 +1,49 @@
+/* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*-
+ *
+ * Copyright (C) 2016 Colin Walters <walters@verbum.org>
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, write to the
+ * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
+ * Boston, MA 02111-1307, USA.
+ */
+
+#pragma once
+
+#ifndef __GI_SCANNER__
+
+#include "ostree-fetcher.h"
+
+G_BEGIN_DECLS
+
+gboolean _ostree_fetcher_mirrored_request_to_membuf (OstreeFetcher *fetcher,
+                                                     GPtrArray     *mirrorlist,
+                                                     const char    *filename,
+                                                     gboolean       add_nul,
+                                                     gboolean       allow_noent,
+                                                     GBytes         **out_contents,
+                                                     guint64        max_size,
+                                                     GCancellable   *cancellable,
+                                                     GError         **error);
+
+gboolean _ostree_fetcher_request_uri_to_membuf (OstreeFetcher *fetcher,
+                                                OstreeFetcherURI *uri,
+                                                gboolean       add_nul,
+                                                gboolean       allow_noent,
+                                                GBytes         **out_contents,
+                                                guint64        max_size,
+                                                GCancellable   *cancellable,
+                                                GError         **error);
+G_END_DECLS
+
+#endif
index c69bbefad7a463ff9a1158fbdcc833bb1c06c9b6..7917f729de4fe59e2a44c55e17d570ad960cb6d2 100644 (file)
@@ -1291,119 +1291,6 @@ _ostree_fetcher_bytes_transferred (OstreeFetcher       *self)
   return ret;
 }
 
-typedef struct
-{
-  GInputStream   *result_stream;
-  gboolean         done;
-  GError         **error;
-}
-FetchUriSyncData;
-
-static void
-fetch_uri_sync_on_complete (GObject        *object,
-                            GAsyncResult   *result,
-                            gpointer        user_data)
-{
-  FetchUriSyncData *data = user_data;
-
-  (void)_ostree_fetcher_request_finish ((OstreeFetcher*)object,
-                                        result, NULL, &data->result_stream,
-                                        data->error);
-  data->done = TRUE;
-}
-
-gboolean
-_ostree_fetcher_mirrored_request_to_membuf (OstreeFetcher  *fetcher,
-                                            GPtrArray     *mirrorlist,
-                                            const char     *filename,
-                                            gboolean        add_nul,
-                                            gboolean        allow_noent,
-                                            GBytes         **out_contents,
-                                            guint64        max_size,
-                                            GCancellable   *cancellable,
-                                            GError         **error)
-{
-  gboolean ret = FALSE;
-  const guint8 nulchar = 0;
-  g_autoptr(GMemoryOutputStream) buf = NULL;
-  g_autoptr(GMainContext) mainctx = NULL;
-  FetchUriSyncData data;
-  g_assert (error != NULL);
-
-  data.result_stream = NULL;
-
-  if (g_cancellable_set_error_if_cancelled (cancellable, error))
-    return FALSE;
-
-  mainctx = g_main_context_new ();
-  g_main_context_push_thread_default (mainctx);
-
-  data.done = FALSE;
-  data.error = error;
-
-  _ostree_fetcher_request_async (fetcher, mirrorlist, filename, 0, max_size,
-                                 OSTREE_FETCHER_DEFAULT_PRIORITY, cancellable,
-                                 fetch_uri_sync_on_complete, &data);
-  while (!data.done)
-    g_main_context_iteration (mainctx, TRUE);
-
-  if (!data.result_stream)
-    {
-      if (allow_noent)
-        {
-          if (g_error_matches (*error, G_IO_ERROR, G_IO_ERROR_NOT_FOUND))
-            {
-              g_clear_error (error);
-              ret = TRUE;
-              *out_contents = NULL;
-            }
-        }
-      goto out;
-    }
-
-  buf = (GMemoryOutputStream*)g_memory_output_stream_new (NULL, 0, g_realloc, g_free);
-  if (g_output_stream_splice ((GOutputStream*)buf, data.result_stream,
-                              G_OUTPUT_STREAM_SPLICE_CLOSE_SOURCE,
-                              cancellable, error) < 0)
-    goto out;
-
-  if (add_nul)
-    {
-      if (!g_output_stream_write ((GOutputStream*)buf, &nulchar, 1, cancellable, error))
-        goto out;
-    }
-
-  if (!g_output_stream_close ((GOutputStream*)buf, cancellable, error))
-    goto out;
-
-  ret = TRUE;
-  *out_contents = g_memory_output_stream_steal_as_bytes (buf);
- out:
-  if (mainctx)
-    g_main_context_pop_thread_default (mainctx);
-  g_clear_object (&(data.result_stream));
-  return ret;
-}
-
-/* Helper for callers who just want to fetch single one-off URIs */
-gboolean
-_ostree_fetcher_request_uri_to_membuf (OstreeFetcher  *fetcher,
-                                       OstreeFetcherURI *uri,
-                                       gboolean        add_nul,
-                                       gboolean        allow_noent,
-                                       GBytes         **out_contents,
-                                       guint64        max_size,
-                                       GCancellable   *cancellable,
-                                       GError         **error)
-{
-  g_autoptr(GPtrArray) mirrorlist = g_ptr_array_new ();
-  g_ptr_array_add (mirrorlist, uri); /* no transfer */
-  return _ostree_fetcher_mirrored_request_to_membuf (fetcher, mirrorlist, NULL,
-                                                     add_nul, allow_noent,
-                                                     out_contents, max_size,
-                                                     cancellable, error);
-}
-
 void
 _ostree_fetcher_uri_free (OstreeFetcherURI *uri)
 {
index 124a02bff4c2ec9a131aec13bbf59aca98a56f90..10ec51dc28b3c0af8749530bc61948c361824146 100644 (file)
@@ -123,24 +123,6 @@ gboolean _ostree_fetcher_request_finish (OstreeFetcher *self,
                                          GInputStream **out_stream,
                                          GError       **error);
 
-gboolean _ostree_fetcher_mirrored_request_to_membuf (OstreeFetcher *fetcher,
-                                                     GPtrArray     *mirrorlist,
-                                                     const char    *filename,
-                                                     gboolean       add_nul,
-                                                     gboolean       allow_noent,
-                                                     GBytes         **out_contents,
-                                                     guint64        max_size,
-                                                     GCancellable   *cancellable,
-                                                     GError         **error);
-
-gboolean _ostree_fetcher_request_uri_to_membuf (OstreeFetcher *fetcher,
-                                                OstreeFetcherURI *uri,
-                                                gboolean       add_nul,
-                                                gboolean       allow_noent,
-                                                GBytes         **out_contents,
-                                                guint64        max_size,
-                                                GCancellable   *cancellable,
-                                                GError         **error);
 G_END_DECLS
 
 #endif
index ee52e51be035a2015382bf066070cc4338579b0a..6afae59e771a14efd258c8b1229917c148df1770 100644 (file)
@@ -21,6 +21,7 @@
 #include "config.h"
 
 #include "ostree-metalink.h"
+#include "ostree-fetcher-util.h"
 #include <gio/gfiledescriptorbased.h>
 
 #include "otutil.h"
index 32ad5fc06c7d669b06e6d64b759e669c9157e35b..100d93048609d227441c8eb4277d33816cab193d 100644 (file)
@@ -32,6 +32,7 @@
 #include "ostree-repo-private.h"
 #include "ostree-repo-static-delta-private.h"
 #include "ostree-metalink.h"
+#include "ostree-fetcher-util.h"
 #include "ot-fs-utils.h"
 
 #include <gio/gunixinputstream.h>